package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;

import com.evon.yardmanagement.service.UtilityService;
import com.evon.yardmanagement.service.VendorManagementService;
import com.evon.yardmanagement.ui.dto.CountryDTO;
import com.evon.yardmanagement.ui.dto.StateDTO;
import com.evon.yardmanagement.ui.dto.VendorDTO;
import com.opensymphony.xwork2.ActionSupport;

public class VendorManagementAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private VendorDTO dto;

	private List<VendorDTO> vendorList;

	private List<CountryDTO> listCountry;

	private int selectedCountryId;

	private List<StateDTO> listState  = new ArrayList<StateDTO>();;

	/**
	 * Action method to load the management screen.
	 * 
	 * @return
	 */
	public String init() {
		vendorList = VendorManagementService.getAllVendors();
		return SUCCESS;
	}
	
	public String back() {
		vendorList = VendorManagementService.getAllVendors();
		return SUCCESS;
	}

	public String showDetails() {
		dto = VendorManagementService.getVendorDetails(dto.getId());
		// populate state list
		listCountry = new UtilityService().getCountries();

		// get OEM country
		selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);

		return SUCCESS;
	}
	
	public String showStates() {
		listCountry = new UtilityService().getCountries();

		// selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);

		return SUCCESS;
	}
	
	public String addVendor(){
		// TODO: move this to global data
		listCountry = new UtilityService().getCountries();
		listState = UtilityService.getState(selectedCountryId);
		//set the state
		
		boolean isUpdate = dto.getId() > 0;
		VendorManagementService.addVendor(dto);
		if (isUpdate) {
			addActionMessage("Successfully Updated the value");
		} else {
			addActionMessage("Added Successfully");
		}
		return SUCCESS;
	}
	
	public String emptyVendor(){
		dto = new VendorDTO();
		listCountry = new UtilityService().getCountries();
		
		return SUCCESS;
	}

	public VendorDTO getDto() {
		return dto;
	}

	public void setDto(VendorDTO dto) {
		this.dto = dto;
	}

	public List<VendorDTO> getVendorList() {
		return vendorList;
	}

	public void setVendorList(List<VendorDTO> vendorList) {
		this.vendorList = vendorList;
	}

	public List<CountryDTO> getListCountry() {
		return listCountry;
	}

	public int getSelectedCountryId() {
		return selectedCountryId;
	}

	public List<StateDTO> getListState() {
		return listState;
	}

	public void setListCountry(List<CountryDTO> listCountry) {
		this.listCountry = listCountry;
	}

	public void setSelectedCountryId(int selectedCountryId) {
		this.selectedCountryId = selectedCountryId;
	}

	public void setListState(List<StateDTO> listState) {
		this.listState = listState;
	}

}
